<?php

class PageController extends Controller {
	public $baseUrl;
	public $isContent;
	public $login;
	public $title;
	public $categories;

	public function init() {
		$this->baseUrl = Yii::app()->request->baseUrl;
		$this->isContent = true;
		$session = new CHttpSession;
		$session->open();
		$this->login = $session['login'];
		$session->close();

		$this->title = 'Контент';
		$this->categories = array(
			'index' => array('title' => 'Страницы', 'url' => CHtml::normalizeUrl(array('page/'))),
			'sliders' => array('title' => 'Слайдеры', 'url' => CHtml::normalizeUrl(array('page/sliders'))),
//            'actions' => array('title' => 'Акции', 'url' => CHtml::normalizeUrl(array('page/info', 'info'=>'actions'))),
//			'news' => array('title' => 'Новости', 'url' => CHtml::normalizeUrl(array('page/info', 'info'=>'news'))),
//			'article' => array('title' => 'Статьи', 'url' => CHtml::normalizeUrl(array('page/info', 'info'=>'article'))),
		);
	}

	//region Управление страницы
	public function actionIndex() {
		$this->categories['index']['active'] = true;
		$data['pages'] = Page::model()->findAll(array('order' => '`title`'));
		$this->render('pages', $data);
	}

	public function actionPage()
	{
		if(Yii::app()->getRequest()->getQuery('id')){
			$data['item']=Page::model()->multilang()->findByPk(Yii::app()->getRequest()->getQuery('id'));
		}else{
			$data=array();
		}
		$this->renderPartial('page',$data);
	}
	public function actionSavePage($id) {
		$item = Yii::app()->getRequest()->getPost('item');

		if($id == 'new'){
			$record = new Page;
			$item['url']=$this->TranslitRuToEn($item['title']);
		}else{
			$record = Page::model()->multilang()->findByPk($id);
		}
		$record->attributes = $item;
		$record->save();
		Yii2Debug::dump($record->getErrors());
		$this->redirect(CHtml::normalizeUrl(array('page/')));
	}
	public function actionDeletedPage($id) {
		Page::model()->deleteByPk($id);
		$this->redirect(CHtml::normalizeUrl(array('page/')));
	}
	//endregion

	//region Управление слайдерами
	public function actionSliders()
	{
		$this->categories['sliders']['active'] = true;
		$data['items']=Sliders::model()->findAll();
		$this->render('sliders',$data);
	}
	public function actionSlider()
	{
		if(Yii::app()->getRequest()->getQuery('id')){
			$data['item']=Sliders::model()->multilang()->findByPk(Yii::app()->getRequest()->getQuery('id'));
		}else{
			$data=array();
		}
		$this->renderPartial('slider',$data);
	}
	public function actionSaveSlider($id)
	{
		$item = Yii::app()->getRequest()->getPost('item');
		if( $id == 'new' ) {
			$record = new Sliders;
			//$record = $record->multilang();
		} else {
			$record = Sliders::model()->multilang()->findByPk($id);
			if ($record->img!=$item['img']) {
				$deleted_img= YiiBase::getPathOfAlias('webroot').$record->img;
			}
		}
		$record->attributes = $item;
		if( !$record->save() ) {
			$file_path = YiiBase::getPathOfAlias('webroot').$item['img'];
			if( file_exists( $file_path ) ) {
				unlink( $file_path );
			}
		}else{
			if(isset($deleted_img)){
				if( file_exists( $deleted_img ) ) {
					unlink( $deleted_img );
				}
			}
		}
		$this->redirect(CHtml::normalizeUrl(array('page/sliders')));
	}
	public function actionDeletedSlider($id)
	{
		$deleted=Sliders::model()->findByPk($id);
		if( file_exists( YiiBase::getPathOfAlias('webroot').$deleted->img ) ) {
			unlink( YiiBase::getPathOfAlias('webroot').$deleted->img );
		}
		$deleted->delete();
		$this->redirect(CHtml::normalizeUrl(array('page/sliders')));
	}

	public function actionSliderVisible($id,$status)
	{
		$item = Sliders::model()->multilang()->findByPk($id);
		$item->isVisible = $status;
		$item->save();
	}
	//endregion

	protected function increment_string($str, $separator = '_', $first = 1)
	{
	    preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
	    return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
	}

}